package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.TbPartner;
import com.heima.mapper.TbPartnerMapper;
import com.heima.pojo.dto.TbPartnerDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.TbPartnerVo;
import com.heima.service.ITbPartnerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 合作商表 服务实现类
 * </p>
 *
 * @author AkiRinko
 * @since 2024-06-06
 */
@Service
@RequiredArgsConstructor
public class TbPartnerServiceImpl extends ServiceImpl<TbPartnerMapper, TbPartner> implements ITbPartnerService {

    private final TbPartnerMapper tbPartnerMapper;

    /**
     * 合作商搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     */
    @Override
    public PageResponse<TbPartnerVo> selectPageList(Integer pageIndex, Integer pageSize, String name) {
        Page<TbPartnerVo> page = new Page<>(pageIndex, pageSize);
        Page<TbPartnerVo> result = tbPartnerMapper.selectPageList(name, page);
        result.getRecords().forEach(vo -> {
            vo.setPassword("");
        });

        return PageResponse.of(result, TbPartnerVo.class);
    }

    /**
     * 新增合作商
     *
     * @param dto
     */
    @Override
    public void insertPartner(TbPartnerDto dto) {
        TbPartner partner = BeanUtil.copyProperties(dto, TbPartner.class);
        tbPartnerMapper.insert(partner);
    }

    /**
     * 获取合作商详情
     *
     * @param id
     */
    @Override
    public TbPartnerVo selectById(Integer id) {
        TbPartnerVo vo = tbPartnerMapper.selectPartnerById(id);
        vo.setPassword("");

        return vo;
    }

    /**
     * 修改合作商
     *
     * @param id
     * @param dto
     */
    @Override
    public void updatePartnerById(Integer id, TbPartnerDto dto) {
        TbPartner partner = BeanUtil.copyProperties(dto, TbPartner.class);

        LambdaQueryWrapper<TbPartner> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbPartner::getId, id);

        tbPartnerMapper.update(partner, wrapper);
    }

    /**
     * 删除合作商
     *
     * @param id
     */
    @Override
    public void deletePartnerById(Integer id) {
        tbPartnerMapper.deleteById(id);
    }

    /**
     * 合作商数量
     *
     * @return
     */
    @Override
    public Integer partnerCount() {
        return count();
    }
}
